/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.context;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Valerio Costmagna <costamagna.valerio@gmail.com>
 */
public class ContextObjectFactory {
    public static RequestObject getRequestObject(HttpServletRequest request)
	{
		RequestObject rObj = new RequestObject();

		//estrae il nome dell'actoion
		String path = request.getServletPath().split(".action")[0];
		if (path.startsWith("/"))
			path = path.substring(1);
		rObj.setRequestCommmand(path);

		//mappa i parametri della request
		Map pMap = request.getParameterMap();
		for (Iterator listaParams = pMap.keySet().iterator(); listaParams.hasNext(); )
		{
			String key = (String) listaParams.next();
			rObj.request.put(key, pMap.get(key));
		}
		//mappa i parametri della sessione
		for (Enumeration listaSession = request.getSession().getAttributeNames(); listaSession.hasMoreElements(); )
		{
			String key = (String) listaSession.nextElement();
                        System.out.println("Mappo nella sessione: "+key);
			rObj.session.put(key, request.getSession().getAttribute(key));
		}

		return rObj;
	}

	public static ResponseObject getResponseObject()
	{
		return new ResponseObject();
	}
}
